package operateBusiness;

import com.alibaba.fastjson.JSONObject;
import models.MyConfig;
import models.ReturnJson;
import models.TestServerBusinessImpl;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * Created by yangxianming on 2023/12/4.
 */
public class ReadExcel implements TestServerBusinessImpl {
    // http://localhost:1988/operateExcel/readExcel?excelPath=D:\javaWorkSpace\laboratory\人员信息.xlsx&excelSheetIndex=0
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        String excelPath = request.getParameter("excelPath");
        String excelSheetIndex = request.getParameter("excelSheetIndex");
        String rowIndex = request.getParameter("rowIndex");
        String cellIndex = request.getParameter("cellIndex");


        System.out.println("excelPath:" + excelPath);


        //判断excelPath是目录还是文件
        File file = new File(excelPath);
        if (file.isDirectory()){
            File[] listFiles = file.listFiles();
            long fileCreateTime = 0l;
            String lastTimeFileName = "";
            for (int i = 0; i < listFiles.length; i++) {
                File onefile = listFiles[i];
                if (onefile.isDirectory()){
                    continue;
                }
                if (fileCreateTime < onefile.lastModified()){
                    fileCreateTime = onefile.lastModified();
                    lastTimeFileName = onefile.getPath();
                }
            }
            System.out.println("最近的文件是:" + lastTimeFileName);

            return getFile_info(lastTimeFileName, excelSheetIndex, rowIndex, cellIndex).toString();
        }

        if (file.isFile()){
            return getFile_info(excelPath, excelSheetIndex, rowIndex, cellIndex).toString();
        }

        ReturnJson returnJson = new ReturnJson();
        returnJson.setApiStatus(0);
        returnJson.setResult("can not found excelPath:" + excelPath);
        return returnJson.toString();
    }

    private ReturnJson getFile_info(String excelPath, String excelSheetIndex, String rowIndex, String cellIndex){
        ReturnJson returnJson = new ReturnJson();
        try {
            int sheetIndex = Integer.valueOf(excelSheetIndex);
//            FileInputStream fis = new FileInputStream("人员信息.xlsx");
            FileInputStream fis = new FileInputStream(excelPath);
            Workbook workbook = new XSSFWorkbook(fis);
            Sheet sheet = workbook.getSheetAt(sheetIndex);

            JSONObject excelMatrix = new JSONObject();

            int lastRowNum = sheet.getLastRowNum();
            for (int r = 0; r < lastRowNum; r++){
                int lastCellNum = sheet.getRow(r).getLastCellNum();
                for (int c = 0; c < lastCellNum; c++){
                    String matrixIndex = c + ";" + r;
                    CellType cellType = sheet.getRow(r).getCell(c).getCellTypeEnum();
//                    System.out.println(cellType.name());
                    if (Objects.equals(cellType.name(), "STRING")){
                        String value = sheet.getRow(r).getCell(c).getStringCellValue();
//                        System.out.print(value);
                        excelMatrix.put(matrixIndex, value);
                    }
                    if (Objects.equals(cellType.name(), "NUMERIC")){
                        DecimalFormat df = new DecimalFormat("0");
                        Number number = sheet.getRow(r).getCell(c).getNumericCellValue();
//                        System.out.print(df.format(number));
                        excelMatrix.put(matrixIndex, df.format(number));
                    }
//                    System.out.print(" ");
                }
//                System.out.println("");
            }
//            System.out.printf(excelMatrix.toString());
            returnJson.setApiStatus(1);
            returnJson.setResult(excelMatrix);
        }catch (Exception e){
//            e.printStackTrace();
            returnJson.setApiStatus(0);
            returnJson.setResult(e.getMessage());
        }
        return returnJson;
    }
}
